Public Class frmPreview


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If PrintDialog1.ShowDialog = DialogResult.OK Then
            'showDialog method makes the dialog box visible at run time
            PrintDocument1.Print()
        End If

    End Sub

    Private Sub frmPreview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.RichTextBox1.Text = "Test nama " & vbCrLf & "test line 2"
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        
        'PrintPage is the foundational printing event. This event gets fired for every
        ' page that will be printed
        Static intCurrentChar As Int32
        ' declaring a static variable to hold the position of the last printed char
        Dim font As New Font("Verdana", 14)
        ' initializing the font to be used for printing
        Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
        With PrintDocument1.DefaultPageSettings
            ' initializing local variables that contain the bounds of the printing area rectangle
            PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            ' initializing local variables to hold margin values that will serve
            ' as the X and Y coordinates for the upper left corner of the printing
            ' area rectangle.
            marginLeft = .Margins.Left
            marginTop = .Margins.Top
            ' X and Y coordinate
        End With

        If PrintDocument1.DefaultPageSettings.Landscape Then
            Dim intTemp As Int32
            intTemp = PrintAreaHeight
            PrintAreaHeight = PrintAreaWidth
            PrintAreaWidth = intTemp
            ' if the user selects landscape mode, swap the printing area height and width
        End If

        Dim pfont As Font = New Font("Arial", 12)
        Dim ppen As New Pen(Color.Black, 3)
        Dim pheight As Integer = CInt(pfont.GetHeight(e.Graphics))
        Dim xpos As Integer = CInt(e.MarginBounds.Left)
        Dim ypos As Integer = CInt(e.MarginBounds.Top)


        Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
        ' calculating the total number of lines in the document based on the height of
        ' the printing area and the height of the font
        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
        ' initializing the rectangle structure that defines the printing area
        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
        'instantiating the StringFormat class, which encapsulates text layout information
        Dim intLinesFilled, intCharsFitted As Int32

        Dim Header As String = "PERBANDANAN PUTRAJAYA"
        font = New Font("Verdana", 18, FontStyle.Bold, GraphicsUnit.Point)
        e.Graphics.DrawString(Header, font, Brushes.Black, xpos, ypos)
        ypos = ypos + CInt(font.GetHeight(e.Graphics))
        e.Graphics.DrawString("APTS & APS", pfont, Brushes.Black, xpos, ypos)
        ypos += pheight
        e.Graphics.DrawString("Printed : " & Date.Now.ToString, New Font("Arial", 10, FontStyle.Italic), Brushes.Black, xpos, ypos)
        ypos += pheight

        e.Graphics.DrawLine(ppen, xpos, ypos, e.MarginBounds.Right - 100, ypos)
        ypos += pheight

        e.Graphics.DrawString("MyKad Information", pfont, Brushes.Black, xpos, ypos)

        ypos += pheight

        'set image
        Dim img As Image = Me.PictureBox1.Image

        'image size in 1/100 inch measurement
        Dim sz As New SizeF(100 * img.Width / img.HorizontalResolution, _
                        100 * img.Height / img.VerticalResolution)
        'Dim p As New PointF((e.PageBounds.Width - sz.Width) / 2, (e.PageBounds.Height - sz.Height) / 2)
        Dim p As New PointF((e.PageBounds.Width - sz.Width) / 2, ypos)
        e.Graphics.DrawImage(img, p)
        'e.Graphics.DrawImage(Me.PictureBox1.Image, New Rectangle(0, 0, Me.PictureBox1.Width, Me.PictureBox1.Height))
        ypos = ypos + (img.Height)
        ypos += pheight
        ypos += pheight
        Dim str As String = Me.TextBox1.Text

        e.Graphics.DrawString("Name : " & Me.TextBox1.Text, pfont, Brushes.Black, xpos, ypos, fmt)
        ypos += pheight
        ypos += pheight

        e.Graphics.DrawString("Name : " & Me.RichTextBox1.Text, pfont, Brushes.Black, xpos, ypos, fmt)
        ypos += pheight

        e.Graphics.MeasureString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
        ' calling MeasureString to determine the number of characters that will fit in
        ' the printing area rectangle
        'e.Graphics.DrawString(" Name : " & Mid(RichTextBox1.Text, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)
        ' print the text to the page
        intCurrentChar += intCharsFitted
        'advancing the current char to the last char printed on this page
        '      If TextBox1.Text.Length Then
        'If intCurrentChar e.HasMorePages=True
        'HasMorePages tells the printing module whether another PrintPage event should be fired
        'Else
        e.HasMorePages = False
        intCurrentChar = 0
        'End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub
End Class